package com.company.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

/**
 * Created by tu on 2017/7/10.
 */
public class JsonUtil {

  public static String toJson(Object src) {
    Gson gson = new GsonBuilder().create();
    return gson.toJson(src);
  }

  public static <T> T fromJson(String json, Class<T> classOfT) {
    Gson gson = new GsonBuilder().create();
    T obj = gson.fromJson(json, classOfT);
    return obj;
  }

  public static <T> T fromJson(String json, TypeToken typeToken) {
    Gson gson = new GsonBuilder().create();
    T obj = gson.fromJson(json, typeToken.getType());
    return obj;
  }
}